package com.dg.river.module.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.dg.river.R;
import com.dg.river.contant.Constant;
import com.dg.river.core.manager.UserInfoManager;
import com.dg.river.core.manager.UserInfoMethod;
import com.dg.river.core.util.EmojiFilterUtils;
import com.dg.river.core.util.TextUtil;
import com.dg.river.core.util.ToastUtils;
import com.dg.river.core.util.Utils;
import com.dg.river.databinding.ActivityForgotPasswordBinding;
import com.dg.river.module.base.BaseActivity;
import com.gyf.immersionbar.ImmersionBar;

/**
 * @作者：HuYong
 * @时间：2023/4/12 16:19
 * @描述：忘记密码
 * @xml：activity_forgot_password.xml
 * @修改：
 */
public class ForgotPasswordActivity extends BaseActivity {

    private ActivityForgotPasswordBinding binding;
    //倒计时
    private long secondCount = Constant.TIMER_SECONDS;
    private Runnable runnable;
    private String account, phone, code, newPwd, newConfirmPwd;

    private static final String EDIT_ACCOUNT_PHONE = "editAccountPhone";

    // 能否编辑账号、手机号
    private boolean editAccountPhone;

    // 编辑内容是否有值
    private boolean editAccountStatus = false;
    private boolean edUsernameStatus = false;
    private boolean edCodeStatus = false;
    private boolean edNewPwdStatus = false;
    private boolean edNewConfirmPwdStatus = false;

    public static void start(Context mContext, boolean editAccountPhone) {
        Intent intent = new Intent(mContext, ForgotPasswordActivity.class);
        intent.putExtra(EDIT_ACCOUNT_PHONE, editAccountPhone);
        mContext.startActivity(intent);
    }

    @Override
    protected void initGetIntent() {
        super.initGetIntent();
        editAccountPhone = getIntent().getBooleanExtra(EDIT_ACCOUNT_PHONE, false);
    }

    @NonNull
    @Override
    public View initLayout() {
        binding = ActivityForgotPasswordBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void initView() {
        //navigationBar:导航栏颜色，不写默认是黑色 statusBar:状态栏的颜色，不写默认透明
        ImmersionBar.with(this)
                .navigationBarColor(R.color.transparent)
                .autoDarkModeEnable(true, 0.2f)
                .statusBarDarkFont(true)
                .keyboardEnable(false).init();

        // 账号、手机号是否可以编辑
        if (editAccountPhone) {
            binding.edAccount.setVisibility(View.VISIBLE);
            binding.edUsername.setVisibility(View.VISIBLE);
            binding.tvAccount.setVisibility(View.GONE);
            binding.tvPhone.setVisibility(View.GONE);
        } else {
            binding.edAccount.setVisibility(View.GONE);
            binding.edUsername.setVisibility(View.GONE);
            binding.tvAccount.setVisibility(View.VISIBLE);
            binding.tvPhone.setVisibility(View.VISIBLE);

            account = TextUtil.isEmptyConvert(UserInfoManager.getInstance().userAccount(UserInfoMethod.GET, ""));
            phone = TextUtil.isEmptyConvert(UserInfoManager.getInstance().userPhone(UserInfoMethod.GET, ""));
            binding.tvAccount.setText(account);
            binding.tvPhone.setText(TextUtil.hideStr(phone, 3, 4));

            editAccountStatus = true;
            edUsernameStatus = true;
            binding.tvSend.setEnabled(true);
        }
    }

    @Override
    public boolean viewAndData() {
        return true;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        binding.includeTitle.mIvBack.setOnClickListener(v -> {
            finishAty();
        });
        // 发送验证码
        binding.tvSend.setOnClickListener(v -> {
            if (editAccountPhone) {
                if (EmojiFilterUtils.hasEmoji(binding.edUsername.getText().toString().trim())) {
                    ToastUtils.getInstance().toast("请不要输入表情符号");
                    binding.tvSend.setEnabled(true);
                    return;
                }
                if (TextUtil.isEmpty(phone)) {
                    ToastUtils.getInstance().toast("请输入手机号");
                    binding.tvSend.setEnabled(true);
                    return;
                }
            }
            binding.tvSend.setEnabled(false);
            if (runnable == null) {
                if (editAccountPhone) {
                    if (checkTelPhone()) {
                        getSendCode();
                    } else {
                        ToastUtils.getInstance().toast("请检查输入手机号是否正确");
                        binding.tvSend.setEnabled(true);
                    }
                } else {
                    getSendCode();
                }
            } else {
                ToastUtils.getInstance().toast("验证码发送中...");
            }
        });
        // 修改
        binding.tvConfirmUp.setOnClickListener(v -> {
            upPassWord();
        });
        // 监听所有输入项已输入内容
        binding.edAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() > 0) {
                    editAccountStatus = true;
                } else {
                    editAccountStatus = false;
                }
                checkBtnStatus();
            }
        });
        binding.edUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() > 0) {
                    phone = s.toString().trim();
                    edUsernameStatus = true;
                    binding.tvSend.setEnabled(true);
                } else {
                    edUsernameStatus = false;
                    binding.tvSend.setEnabled(false);
                }
                checkBtnStatus();
            }
        });
        binding.edCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() > 0) {
                    edCodeStatus = true;
                } else {
                    edCodeStatus = false;
                }
                checkBtnStatus();
            }
        });
        binding.edNewPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() > 0) {
                    edNewPwdStatus = true;
                } else {
                    edNewPwdStatus = false;
                }
                checkBtnStatus();
            }
        });
        binding.edNewConfirmPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() > 0) {
                    edNewConfirmPwdStatus = true;
                } else {
                    edNewConfirmPwdStatus = false;
                }
                checkBtnStatus();
            }
        });
    }

    /**
     * 校验按钮是否可用
     */
    private void checkBtnStatus() {
        if (editAccountStatus && edUsernameStatus && edCodeStatus && edNewPwdStatus && edNewConfirmPwdStatus) {
            binding.tvConfirmUp.setBackground(ContextCompat.getDrawable(mContext, R.drawable.sh_all_blue_10));
            binding.tvConfirmUp.setEnabled(true);
        } else {
            binding.tvConfirmUp.setBackground(ContextCompat.getDrawable(mContext, R.drawable.sh_all_blue_10_4));
            binding.tvConfirmUp.setEnabled(false);
        }
    }

    /**
     * 发送验证码
     */
    private void getSendCode() {

    }

    /**
     * 修改密码
     */
    private void upPassWord() {
        if (editAccountPhone) {
            if (EmojiFilterUtils.hasEmoji(binding.edAccount.getText().toString().trim())) {
                ToastUtils.getInstance().toast("请不要输入表情符号");
                return;
            }
            if (EmojiFilterUtils.hasEmoji(binding.edUsername.getText().toString().trim())) {
                ToastUtils.getInstance().toast("请不要输入表情符号");
                return;
            }
        }
        if (EmojiFilterUtils.hasEmoji(binding.edCode.getText().toString().trim())) {
            ToastUtils.getInstance().toast("请不要输入表情符号");
            return;
        }
        if (EmojiFilterUtils.hasEmoji(binding.edNewPwd.getText().toString().trim())) {
            ToastUtils.getInstance().toast("请不要输入表情符号");
            return;
        }
        if (EmojiFilterUtils.hasEmoji(binding.edNewConfirmPwd.getText().toString().trim())) {
            ToastUtils.getInstance().toast("请不要输入表情符号");
            return;
        }
        code = binding.edCode.getText().toString().trim();
        newPwd = binding.edNewPwd.getText().toString().trim();
        newConfirmPwd = binding.edNewConfirmPwd.getText().toString().trim();
        if (editAccountPhone) {
            account = binding.edAccount.getText().toString().trim();
            phone = binding.edUsername.getText().toString().trim();
            if (TextUtil.isEmpty(account)) {
                ToastUtils.getInstance().toast("请输入账号");
                return;
            }
            if (TextUtil.isEmpty(phone)) {
                ToastUtils.getInstance().toast("请输入手机号");
                return;
            }
            if (!Utils.checkPhoneNum(phone)) {
                ToastUtils.getInstance().toast("请检查手机号");
                return;
            }
        }
        if (TextUtil.isEmpty(code)) {
            ToastUtils.getInstance().toast("请输入验证码");
            return;
        }
        if (TextUtil.isEmpty(newPwd)) {
            ToastUtils.getInstance().toast("请输入密码");
            return;
        }
        if (newPwd.length() < 6) {
            ToastUtils.getInstance().toast("请输入最少6位密码");
            return;
        }
        if (TextUtil.inputJudge(newPwd)) {
            ToastUtils.getInstance().toast("密码不能包含特殊字符");
            return;
        }
        if (!TextUtil.isEqual(newPwd, newConfirmPwd)) {
            ToastUtils.getInstance().toast("请确认两次输入密码一致");
            return;
        }
        if (TextUtil.inputJudge(newConfirmPwd)) {
            ToastUtils.getInstance().toast("密码不能包含特殊字符");
            return;
        }
        confirm();
    }

    /**
     * 执行忘记密码
     */
    private void confirm() {

    }

    /**
     * 倒计时计算
     */
    Handler handler = new Handler();

    private String ComputeTime() {
        String backTime = "";
        secondCount--;
        if (secondCount <= 0) {
            backTime = "重新获取";
            binding.tvSend.setEnabled(true);
            secondCount = Constant.TIMER_SECONDS;
            handler.removeCallbacks(runnable);
            runnable = null;
        } else {
            backTime = secondCount + "秒";
        }
        return backTime;
    }

    /**
     * 检查电话号码是否正确
     *
     * @return
     */
    private boolean checkTelPhone() {
        boolean emptyTel = TextUtil.isEmpty(EmojiFilterUtils.filterEmoji(phone));
        if (!emptyTel && phone.trim().length() == 11) {
            if ("1".equals(phone.trim().substring(0, 1))) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public void showErrorMessage(@Nullable String message) {

    }
}
